package com.jubao.ejb.business.impl;

import java.util.List;

import javax.ejb.Stateless;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.business.Business;
import com.jubao.ejb.business.UserBusinessLocal;
import com.jubao.ejb.business.UserBusinessRemote;
import com.jubao.ejb.business.util.BusinessConstant;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.Coupon;
import com.jubao.ejb.entity.CouponItem;
import com.jubao.ejb.entity.GeneRelation;
import com.jubao.ejb.entity.Product;
import com.jubao.ejb.entity.Store;
import com.jubao.ejb.entity.User;
import com.jubao.ejb.entity.UserWebsite;
import com.jubao.ejb.entity.Website;

@Stateless
@LocalBinding(jndiBinding = BusinessConstant.UserBusinessLocalJndiName)
@RemoteBinding(jndiBinding = BusinessConstant.UserBusinessRemoteJndiName)
public class UserBusinessImpl extends Business implements UserBusinessLocal, UserBusinessRemote {

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.UserBusiness#saveUserManagedStore(long, com.jubao.ejb.entity.Store)
	 */
	public User saveManagedStore(long userSysId, Store store) {
		User user = userDao.find(userSysId);
		if (user != null) {
			//TODO for first stage, always enable store 
			store.setStatus(BusinessConstant.Status.IS_APPROVE_SUCC.getId());
			Store savedStore = storeDao.save(store);
			if (savedStore != null) {
				user.setManagedStore(savedStore);
				user = userDao.save(user);
			}
		}

		return user;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.UserBusiness#deleteUserFavoriteWebSite(long, long)
	 */
	public void deleteUserFavoriteWebSite(long userSysId, long websiteSysId) {
		UserWebsite userWebsite = this.findFavoriteWebsite(userSysId, websiteSysId);
		if (userWebsite != null) {
			userWebsite.setRank(-1);
			userWebsiteDao.save(userWebsite);
		}

	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.UserBusiness#findFavoriteWebsite(long, long)
	 */
	public UserWebsite findFavoriteWebsite(long userSysId, long websiteSysId) {
		return userWebsiteDao.find(userSysId, BusinessConstant.RelationID.USER_IS_FAVORITER_OF_WEBSITE.getId(),
				websiteSysId);
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.UserBusiness#getFavoriteWebSiteList(long, int, int)
	 */
	public List<UserWebsite> getFavoriteWebSiteList(long userSysId, int pgno, int pgsize) {
		return userWebsiteDao.getUserWebsiteList(userSysId, BusinessConstant.RelationID.USER_IS_FAVORITER_OF_WEBSITE
				.getId(), -1, pgno, pgsize);
	}

	public long getFavoriteWebSiteCount(long userSysId) {
		return userWebsiteDao.getUserWebsiteCount(userSysId, BusinessConstant.RelationID.USER_IS_FAVORITER_OF_WEBSITE
				.getId(), -1);
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.UserBusiness#saveFavoriteWebsite(long, long, com.jubao.ejb.entity.UserWebsite)
	 */
	public UserWebsite saveFavoriteWebsite(long userSysId, long websiteSysId, UserWebsite userWebsite) {
		User user = userDao.find(userSysId);
		Website website = websiteDao.find(websiteSysId);
		if (user != null && website != null && userWebsite != null) {
			userWebsite.setUser(user);
			userWebsite.setRelationId(BusinessConstant.RelationID.USER_IS_FAVORITER_OF_WEBSITE.getId());
			userWebsite.setWebsite(website);
			return userWebsiteDao.save(userWebsite);
		}
		return null;
	}

	public List<User> getServantList(long areaCategorySysId, long businessCategorySysId, int pgno, int pgsize) {
		int relationId1 = areaCategorySysId > -1 ? BusinessConstant.RelationID.USER_IS_SERVANT_MEMBER_OF_CATEGORY
				.getId() : -1;
		int relationId2 = businessCategorySysId > -1 ? BusinessConstant.RelationID.USER_IS_SERVANT_MEMBER_OF_CATEGORY
				.getId() : -1;

		List stores = super.geneRelationDao.getEntityList(User.class.getName(), relationId1, areaCategorySysId,
				relationId2, businessCategorySysId, pgno, pgsize);
		return stores;
	}

	public long getServantCount(long areaCategorySysId, long businessCategorySysId) {
		int relationId1 = areaCategorySysId > -1 ? BusinessConstant.RelationID.USER_IS_SERVANT_MEMBER_OF_CATEGORY
				.getId() : -1;
		int relationId2 = businessCategorySysId > -1 ? BusinessConstant.RelationID.USER_IS_SERVANT_MEMBER_OF_CATEGORY
				.getId() : -1;

		Long result = super.geneRelationDao.getEntityCount(User.class.getName(), relationId1, areaCategorySysId,
				relationId2, businessCategorySysId);
		return result;
	}

	public long getServeStoreCount(long userSysId, long storeSysId) {
		int relationId1 = BusinessConstant.RelationID.USER_IS_SERVANT_OF_STORE.getId();
		Long result = super.geneRelationDao.getEntity2Count(userSysId, relationId1, storeSysId, Store.class.getName());
		return result;
	}

	public List<Store> getServeStoreList(long userSysId, long storeSysId, int pgno, int pgsize) {
		int relationId1 = BusinessConstant.RelationID.USER_IS_SERVANT_OF_STORE.getId();
		List stores = super.geneRelationDao.getEntity2List(userSysId, relationId1, storeSysId, Store.class.getName(),
				pgno, pgsize);
		return stores;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.UserBusiness#addServant2CateogryTrail(long, long)
	 */
	public void addServant2CateogryTrail(long userSysId, long categorySysId) {
		User user = userDao.find(userSysId);
		List<Category> primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(categorySysId);
		if (user != null && primaryCategoryTrail != null && primaryCategoryTrail.size() > 0) {
			for (int i = 0; i < primaryCategoryTrail.size(); i++) {
				Category category = primaryCategoryTrail.get(i);
				GeneRelation gr = geneRelationDao.find(userSysId,
						BusinessConstant.RelationID.USER_IS_SERVANT_MEMBER_OF_CATEGORY.getId(), category.getSysId());
				if (gr == null) {
					gr = new GeneRelation();
					gr.setEntity(user);
					gr.setRelationId(BusinessConstant.RelationID.USER_IS_SERVANT_MEMBER_OF_CATEGORY.getId());
					gr.setEntity2(category);
					geneRelationDao.save(gr);
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.UserBusiness#removeServantFromCategoryTrail(long, long)
	 */
	public void removeServantFromCategoryTrail(long userSysId, long categorySysId) {
		User website = userDao.find(userSysId);
		List<Category> primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(categorySysId);
		if (website != null && primaryCategoryTrail != null && primaryCategoryTrail.size() > 0) {
			for (int i = 0; i < primaryCategoryTrail.size(); i++) {
				Category category = primaryCategoryTrail.get(i);
				GeneRelation gr = geneRelationDao.find(userSysId,
						BusinessConstant.RelationID.USER_IS_SERVANT_MEMBER_OF_CATEGORY.getId(), category.getSysId());
				if (gr != null) {
					geneRelationDao.remove(gr);
				}
			}
		}
	}


	public long getSuppliedProductCount(long userSysId) {
		return productDao.getProductCount(userSysId);
	}

	public List<Product> getSuppliedProductList(long userSysId, int pgno, int pgsize) {
		List<Product> products = productDao.getProductList(userSysId, pgno, pgsize);
		return products;
	}

	public Product registerSuppliedProductProfile(long supplierUserSysId, Product product) {
		Product result = null;
		User supplier = userDao.find(supplierUserSysId);
		if (supplier != null && product != null && product.getId() != null) {
			result = productDao.find(product.getId());
			if (result == null) {
				product.setSupplier(supplier);
				result = productDao.save(product);
			}
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.UserBusiness#isProductSupplier(long, long)
	 */
	public boolean isProductSupplier(long userSysId, long productSysId) {
		Product product = productDao.find(productSysId);
		if (product != null && product.getSupplier() != null && product.getSupplier().getSysId() == userSysId)
			return true;
		return false;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.UserBusiness#isStoreServant(long, long)
	 */
	public boolean isStoreServant(long userSysId, long storeSysId) {
		long count = this.getServeStoreCount(userSysId, storeSysId);
		return count > 0;
	}

}
